﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Web.Models;
using Common.Unit;
using GD.Model;

namespace Web.Controllers
{
    public class BaseController : Controller
    {
        private GDAdmin_Model login_user;
        /// <summary>
        /// 当前登录的用户对象模型
        /// </summary>
        public GDAdmin_Model CurrentLoginedUser
        {
            get
            {
                if (login_user == null)
                {
                    login_user = HttpContext.Session.CurrentUser();
                }
                return login_user;
            }
            set
            {
                HttpContext.Session.Set(SessionNames.User_Model, value);
                login_user = value;
            }
        }

        /// <summary>
        /// 当前登录的用户ID
        /// </summary>
        public int CurrentUserID
        {
            get
            {
                if (CurrentLoginedUser == null) return 0;
                return CurrentLoginedUser.Id.Value;
            }
        }

        /// <summary>
        /// 当前用户是否登录
        /// </summary>
        public bool IsLogined
        {
            get { return CurrentLoginedUser != null; }
        }

        /// <summary>
        /// 存取验证码
        /// </summary>
        public string CurrentUserCode
        {
            get
            {
                return HttpContext.Session.Get<string>(SessionNames.User_Code);
            }
            set
            {
                HttpContext.Session.Set(SessionNames.User_Code, value);
            }
        }



        #region AJAX相关        
        /// <summary>
        /// AJAX结操作结果
        /// </summary>
        /// <param name="success"></param>
        /// <param name="msg"></param>
        protected JsonResult ajaxMsg(bool success, string msg)
        {
            return ajaxMsg(success, msg, null);
        }
        /// <summary>
        /// AJAX结操作结果
        /// </summary>
        /// <param name="success"></param>
        /// <param name="msg"></param>
        /// <param name="data">json数据 </param>
        protected JsonResult ajaxMsg(bool success, string msg, int? state)
        {
            return CommonJsonResult.NewResult(success, msg, state).JsonResult;
        }
        protected JsonResult ajaxMsg(bool success, string msg, int? state, object obj)
        {
            return CommonJsonResult.NewResult(success, msg, state, obj).JsonResult;
        }
        protected JsonResult ajaxMsg(CommonJsonResult jsonResult)
        {
            return jsonResult.JsonResult;
        }
        #endregion

    }
}